<template>
	<div class="boxStyle">
		<el-row type="flex" class="row-bg" justify="start" style="margin-bottom: 10px;">
			<el-col :span="1" v-if="addShow!==-1">
				<el-button type="warning" size="small" @click="add" icon="el-icon-plus">新增</el-button>
			</el-col>
		</el-row>
		<el-table ref="multipleTable" border stripe :data="tableLists" tooltip-effect="dark" style="margin-bottom: 10px;width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="name" label="分类名称" width="120"></el-table-column>
			<el-table-column prop="remark" label="备注" width="120"></el-table-column>
			<el-table-column prop="sort" label="排序" width="120"></el-table-column>
			<el-table-column prop="createdTime" label="添加时间" :formatter="dateFormat" width="180"></el-table-column>
			<el-table-column prop="id" label="链接" width="300"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" @click="update(scope.$index,scope.row.id)" v-if="updateShow!=-1">更新信息</el-button>
					<el-button size="mini" type="danger" @click="del(scope.row.id)" v-if="deleShow!==-1&&!scope.row.defaultFlag">删除信息</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="size" layout="prev, pager, next,total, slot" :total="totals">
			<span style="margin-top: -2px;color: #606266;font-weight: 100;">第<el-input type="text" v-model="inputPage" style="width: 60px;margin: 0 5px;"/>页<el-button size="mini" style="background: #409EFF; color: #fff;margin:0 5px;" @click="handleCurrentChange(inputPage)">跳转</el-button><span>总计{{totalPage}}页</span></span>
		</el-pagination>
		<div class="modal fade" id="gType" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:800px;height: 340px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
						<ul class="nav nav-tabs gType" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">新增/编辑</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:210px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="rulegType" :rules="rules" ref="rulegType" label-width="100px" class="demo-ruleForm" @submit.native.prevent>
									<input type="hidden" name="id" v-model="id">
									<el-col :span="12">
										<el-form-item label="名称:" prop="name">
											<el-input v-model="rulegType.name" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="备注:" prop="remark">
											<el-input v-model="rulegType.remark" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="排序:" prop="sort">
											<el-input v-model.number="rulegType.sort" size="small"></el-input>
										</el-form-item>
									</el-col>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-success" id="save" @click="save('rulegType')">保存</button>
						<button type="button" class="btn btn-success" id="usave" @click="uSave('rulegType')">保存</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
	</div>
</template>

<script>
	import axios from "axios"
	import { formatDate } from 'static/js/date.js'
	import qs from "qs"
	export default {
		data() {
			return {
				tableLists: [],
				purl: this.$store.state.purl,
				currentPage: 1,
				url: this.GLOBAL.url,
				imgurl:this.GLOBAL.imgurl,
				totals: 0, //分页总条数
				size: 0, //每页条数
				updateShow: 0, //是否显示更新信息
				addShow: 0, //是否显示新增
				deleShow: 0,
				id: "",
				rulegType: {
					name: "",
					remark: "",
					sort: null,
				},
				rules: {
					name: [{
							required: true,
							message: '请输入名称',
							trigger: 'change'
						},
						{
							max: 6,
							message: '名称不能超过6个字符'
						},
						{
							pattern: /^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$/,
							message: '名称不能有特殊字符'
						}
					],
					remark: [{
							max: 20,
							message: '名称不能超过20个字符'
						},
						{
							pattern: /^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$/,
							message: '备注不能有特殊字符'
						}
					],
					sort: [{
						required: true,
						message: '排序不能为空',
						trigger: 'change'
					}, {
						type: 'number',
						message: '排序只能为数字'
					}],
				},
				inputPage: 1,
				totalPage: 0, //总页数
				adimg: ""
			}
		},
		mounted() {
			this.load(1);
			this.inputPage = this.currentPage;
			this.addShow = this.purl[0].findIndex((val) => val === "/interface/goodstype/add") //判断是否拥有增加权限
			this.updateShow = this.purl[0].findIndex((val) => val === "/interface/goodstype/view") //判断是否拥有更新权限
			this.deleShow = this.purl[0].findIndex((val) => val === "/interface/goodstype/add")
		},
		methods: {
			load(num) {
				axios.get("interface/goodstype/list?pageNo=" + num).then((res) => {
					this.tableLists = res.data.data.list; //list数据
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			handleCurrentChange(val) {
				this.currentPage = val - 0;
				this.inputPage = val;
				load(val)
			},
			add() {
				$("#gType").modal("show")
				$("#save").show();
				this.rulegType.name = "";
				this.rulegType.remark = "";
				this.rulegType.sort = null;
				$("#usave").hide();
			},
			save(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						var params = {
							name: this.rulegType.name,
							remark: this.rulegType.remark,
							sort: this.rulegType.sort,
						}
						axios({
							method: 'post',
							url: '/interface/goodstype/add',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.load(this.currentPage);
								$("#gType").modal("hide");
								this.$message.success("新增成功");
							} else {
								this.$message.error(res.data.msg)
							}

						});
					} else {
						return false;
					}
				})
			},
			update(index, id) {
				$("#gType").modal("show")
				$("#save").hide();
				$("#usave").show();
				this.rulegType.name = this.tableLists[index].name;
				this.rulegType.remark = this.tableLists[index].remark;
				this.rulegType.sort = this.tableLists[index].sort;
				this.id = id;
			},
			uSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						var params = {
							name: this.rulegType.name,
							remark: this.rulegType.remark,
							sort: this.rulegType.sort,
							id: this.id,
						}
						axios({
							method: 'post',
							url: '/interface/goodstype/add',
							data: params,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.load(this.currentPage);
								$("#gType").modal("hide");
								this.$message.success("更新成功");
							} else {
								this.$message.error(res.data.msg)
							}
						});
					} else {
						return false;
					}
				})
			},
			del(id) {
				this.$confirm('确认要删除此条数据吗?', '删除警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					axios({
						method: 'post',
						url: '/interface/goodstype/add',
						data: {
							"id": id,
							"deleteFlag": 1
						},
						headers: {
							'Content-Type': 'application/json'
						}
					}).then((res) => {
						this.load(1)
					})
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				}).catch(() => {});
			},
			dateFormat(row, column) {
				var date
				if(row[column.property]) {
					date = new Date(parseInt(row[column.property]))
					date = formatDate(date, 'yyyy-MM-dd hh:mm:ss')
				}
				if(date == undefined) {
					return "";
				}
				return date;
			},
		}
	}
</script>

<style>
	#gType .modal-dialog {
		left: 25% !important;
		top: 15% !important;
	}
</style>